import {Component, OnInit, ViewChild} from '@angular/core';
import {ActivatedRoute} from "@angular/router";
import {HttpClient, HttpHeaders} from "@angular/common/http";

@Component({
  selector: 'app-goodsdetails',
  templateUrl: './goodsdetails.component.html',
  styleUrls: ['./goodsdetails.component.scss']
})
export class GoodsdetailsComponent implements OnInit {
  // 根据图片id筛选图片信息
  public id: any;
  // 存放商品
  public goodsList: any = [];
  // 存放商品信息
  public goodsMsg: any = [];
  public more: any = 'assets/more/u742.png';
  public cancel: any = 'assets/cancel/u988.png';
  public title: any = '商品详情';
  // 控制显示购买数量页面
  public show: any = 0;
  // 购物车商品的数量
  public num: any = 0;
  // 控制购物车小圆点
  public count: any = false;
  // 控制商品详情显示隐藏
  public showGoodsdetail: any = true;
  // 控制评价显示隐藏
  public showComment: any = false;
  // 添加购物车成功
  public successAddShopcar: any = false;
  // 存放留言接口
  public appraiseList: any = [];
  @ViewChild('msg1', {static: true}) child;
  constructor( private router: ActivatedRoute, private http: HttpClient) {
  }
  ngOnInit() {
    window.scroll(0, 0);
    this.router.params.subscribe( result => {
      console.log("++++++"+result.id);
      if ( result !== null) {
        this.id = result.id;
      }
    });
    const url = 'http://192.168.2.228:8080/goods/list';
    this.http.get(url).subscribe( (result) => {
      this.goodsList = result['data']['list'];
      if ( this.goodsList !== '') {
        for ( let i = 0; i < this.goodsList.length; i++) {
          if ( this.id == this.goodsList[i].g_id) {
            this.goodsMsg = this.goodsList[i];
          }
        }
      }
    }, error2 => {
      console.log("数据请求失败");
    });
  }
  doAddshopcar() {
    // 变成单数 为加入购物车
    this.show = this.show * 2 + 1 ;
  }
  // 自动跳转， 结合output
  runParent( msg) {
    this.count = true;
   let msg1 = msg.split(',');
    this.num = msg1[0];
    setTimeout(() => {
      this.successAddShopcar = msg[1];
    }, 200);
    setTimeout(() => {
      this.successAddShopcar = false;
    }, 2000);
  }
  // 去购买
  doPayMoney() {
    // 显示选择数量
    this.show = this.show * 2 + 2 ;
  }
  goGoodsDetails() {
    this.showGoodsdetail = true;
    this.showComment = false;
  }
  goComment() {
    this.showGoodsdetail = false;
    this.showComment = true;
    // 调用评论接口
    const url = 'http://192.168.2.228:8080/appraise/list?goods_id=' + this.id;
    this.http.get(url).subscribe( res => {
      this.appraiseList = res['data']['list'];
    });

  }

}
